package com.ljkj.cordial.chat.dao.notify;

import android.text.TextUtils;

import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.dao.DbController;
import com.ljkj.cordial.chat.dao.GroupMemberTableDao;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 群成员
 */
@Entity(nameInDb = "GroupMemberTable")
public class GroupMemberTable {
    public final static int MEMBER = 0;
    public final static int OWNER = 1;
    @Id(autoincrement = true)
    private Long id;


    // 群成员ID
    @Property(nameInDb = "GROUP_MEMBER_ID")
    private String groupMemberId;

    // 群成员用户ID
    @Property(nameInDb = "GROUP_MEMBER_USER_ID")
    private String groupMemberUserId;

    // 群成员名称
    @Property(nameInDb = "GROUP_MEMBER_NAME")
    private String groupMemberName;

    // 群成员头像
    @Property(nameInDb = "GROUP_MEMBER_AVATAR")
    private String groupMemberAvatar;
    // 群成员头像本地id
    @Property(nameInDb = "GROUP_MEMBER_AVATAR_ID")
    private String groupMemberAvatarId;
    // 群成员头像本地id
    @Property(nameInDb = "GROUP_MEMBER_AVATAR_Path")
    private String groupMemberAvatarPath;
    // 群成员身份(0:成员，1:群主)
    @Property(nameInDb = "GROUP_MEMBER_TYPE")
    private int groupMemberType;

    // 群成员昵称
    @Property(nameInDb = "GROUP_MEMBER_NICK")
    private String groupMemberNick;

    // 群成员自定义备注
    @Property(nameInDb = "GROUP_MEMBER_ALIAS")
    private String groupMemberAlias;

    // 群成员用户类型
    @Property(nameInDb = "USER_TYPE")
    private String userType;

    // 在线状态
    @Property(nameInDb = "ONLINE_STATUS")
    private String onlineStatus;
    @Property(nameInDb = "GROUP_ID")
    private String groupId;
    @Property(nameInDb = "GROUP_NAME")
    private String groupName;
    @Property(nameInDb = "GROUP_TYPE")
    private String groupType;
    @Property(nameInDb = "LOGIN_USER_ID")
    private String loginUserId;

    @Generated(hash = 1320155007)
    public GroupMemberTable() {
    }


    @Generated(hash = 1587673867)
    public GroupMemberTable(Long id, String groupMemberId, String groupMemberUserId, String groupMemberName, String groupMemberAvatar,
                            String groupMemberAvatarId, String groupMemberAvatarPath, int groupMemberType, String groupMemberNick,
                            String groupMemberAlias, String userType, String onlineStatus, String groupId, String groupName, String groupType,
                            String loginUserId) {
        this.id = id;
        this.groupMemberId = groupMemberId;
        this.groupMemberUserId = groupMemberUserId;
        this.groupMemberName = groupMemberName;
        this.groupMemberAvatar = groupMemberAvatar;
        this.groupMemberAvatarId = groupMemberAvatarId;
        this.groupMemberAvatarPath = groupMemberAvatarPath;
        this.groupMemberType = groupMemberType;
        this.groupMemberNick = groupMemberNick;
        this.groupMemberAlias = groupMemberAlias;
        this.userType = userType;
        this.onlineStatus = onlineStatus;
        this.groupId = groupId;
        this.groupName = groupName;
        this.groupType = groupType;
        this.loginUserId = loginUserId;
    }


    public String getOnlineStatus() {
        return this.onlineStatus;
    }

    public void setOnlineStatus(String onlineStatus) {
        this.onlineStatus = onlineStatus;
    }

    public String getGroupId() {
        return this.groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getGroupName() {
        return this.groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getGroupType() {
        return this.groupType;
    }

    public void setGroupType(String groupType) {
        this.groupType = groupType;
    }


    public static long insert(GroupMemberTable memberTable) {
        GroupMemberTable m = query(memberTable.getGroupId(), memberTable.getGroupMemberUserId());
        if (m != null) {
            memberTable.setId(m.id);
        }
        memberTable.setLoginUserId(ChatApp.getInstance().getUserId());
        return DbController.getInstance().getWrite().insertOrReplace(memberTable);
    }

    public static long insertOrUpdate(GroupMemberTable memberTable) {
        if (memberTable.getId() != null) {
            DbController.getInstance().getWrite().update(memberTable);
            return memberTable.getId();
        }
        memberTable.setLoginUserId(ChatApp.getInstance().getUserId());
        return DbController.getInstance().getWrite().insert(memberTable);
    }

    public static long update(GroupMemberTable memberTable) {
        if (memberTable.getId() != null) {
            memberTable.setLoginUserId(ChatApp.getInstance().getUserId());
            DbController.getInstance().getWrite().getGroupMemberTableDao().update(memberTable);
            return memberTable.getId();
        }

        return -1;
    }

    public static void insertOrUpdate(List<GroupMemberTable> memberTables) {
        for (GroupMemberTable memberTable : memberTables) {
            insertOrUpdate(memberTable);
        }
    }

    public static void update(List<GroupMemberTable> memberTables) {
        DbController.getInstance().getWrite().getGroupMemberTableDao().updateInTx(memberTables);
    }

    public static GroupMemberTable query(String groupId, String groupMemberUserId) {
        return DbController.getInstance().getRead().getGroupMemberTableDao()
                .queryBuilder().where(GroupMemberTableDao.Properties.GroupId.eq(groupId),
                        GroupMemberTableDao.Properties.GroupMemberUserId.eq(groupMemberUserId)
                        , GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();

    }

    public static GroupMemberTable create(String groupId, String groupMemberUserId) {
        GroupMemberTable u = DbController.getInstance().getRead().getGroupMemberTableDao()
                .queryBuilder().where(GroupMemberTableDao.Properties.GroupId.eq(groupId),
                        GroupMemberTableDao.Properties.GroupMemberUserId.eq(groupMemberUserId)
                        , GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();
        return u == null ? new GroupMemberTable() : u;
    }

    public static List<GroupMemberTable> query(String groupId) {
        return DbController.getInstance().getRead().getGroupMemberTableDao()
                .queryBuilder().where(GroupMemberTableDao.Properties.GroupId.eq(groupId),
                        GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).list();
    }

    public static List<GroupMemberTable> queryMembers(String memberUserId) {
        return DbController.getInstance().getRead().getGroupMemberTableDao()
                .queryBuilder().where(GroupMemberTableDao.Properties.GroupMemberUserId.eq(memberUserId),
                        GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).list();
    }

    public static List<GroupMemberTable> queryNoUser(String groupId) {
        return DbController.getInstance().getRead().getGroupMemberTableDao()
                .queryBuilder().where(GroupMemberTableDao.Properties.GroupId.eq(groupId),
                        GroupMemberTableDao.Properties.GroupMemberUserId.notEq(ChatApp.getInstance().getUserId()),
                        GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).list();
    }

    public static List<GroupMemberTable> search(String keyword) {
        keyword = "%" + keyword + "%";
        return DbController.getInstance().getRead()
                .getGroupMemberTableDao().queryBuilder()
                .where(GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId()))
                .whereOr(GroupMemberTableDao.Properties.GroupMemberName.like(keyword), GroupMemberTableDao.Properties.GroupMemberAlias.like(keyword), GroupMemberTableDao.Properties.GroupMemberUserId.like(keyword))
                .list();
    }

    public static void insert(List<GroupMemberTable> memberTables) {
        if (memberTables == null || memberTables.isEmpty()) {
            return;
        }
        for (GroupMemberTable memberTable : memberTables) {
            GroupMemberTable g = DbController.getInstance().getRead().getGroupMemberTableDao()
                    .queryBuilder().where(GroupMemberTableDao.Properties.GroupMemberId.eq(memberTable.getGroupMemberId())
                            , GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();
            if (g != null) {
                memberTable.setId(g.id);
            }
            memberTable.setLoginUserId(ChatApp.getInstance().getUserId());
        }
        DbController.getInstance().getWrite().getGroupMemberTableDao().insertOrReplaceInTx(memberTables);
    }


    /**
     * 获取群头像ids
     *
     * @param groupId
     * @return
     */
    public static Map<String, String> getMemberAvatarPaths(String groupId) {
        Map<String, String> avatars = new HashMap<>();
        List<GroupMemberTable> list = DbController.getInstance().getRead().getGroupMemberTableDao()
                .queryBuilder().where(GroupMemberTableDao.Properties.GroupId.eq(groupId)
                        , GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).list();
        if (list != null && !list.isEmpty()) {
            int len = list.size() > 9 ? 9 : list.size();
            for (int i = 0; i < len; i++) {
                GroupMemberTable b = list.get(i);
                if (!TextUtils.isEmpty(b.getGroupMemberAvatar()) && !TextUtils.isEmpty(b.getGroupMemberAvatarPath())) {
                    avatars.put(b.getGroupMemberUserId(), b.getGroupMemberAvatar() + "," + b.getGroupMemberAvatarPath());
                }
            }
        }
        return avatars;
    }


    @Deprecated
    public static String[] getAvatarEntityLoc(String groupId) {
        return null;
//        Map<String, String> avatars = getMemberAvatarPaths(groupId);
//        if (avatars.isEmpty()) {
//            return null;
//        }
//
//        List<String> ids = new ArrayList<>();
//        List<String> urls = new ArrayList<>();
//        List<String> paths = new ArrayList<>();
//        for (Map.Entry<String, String> entity : avatars.entrySet()) {
//            if (!TextUtils.isEmpty(entity.getValue())) {
//                ids.add(entity.getKey());
//                String[] s = entity.getValue().split(",");
//                urls.add(s[0]);
//                paths.add(s[1]);
//            }
//
//        }
//        String[] v = new String[3];
//        v[0] = TextUtils.join(",", ids);
//        v[1] = TextUtils.join(",", urls);
//        v[2] = TextUtils.join(",", paths);
//        return v;
    }

    public static boolean deleteByUserId(String groupId, String groupMemberUserId) {
        GroupMemberTable b = DbController.getInstance().getRead().getGroupMemberTableDao().queryBuilder()
                .where(GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId()
                        ), GroupMemberTableDao.Properties.GroupMemberUserId.eq(groupMemberUserId)
                        , GroupMemberTableDao.Properties.GroupId.eq(groupId)).unique();
        if (b != null) {
            DbController.getInstance().getWrite().getGroupMemberTableDao().delete(b);
            return true;
        } else{
            return false;
        }
    }

    public static void deleteByGroupId(String groupId) {
        List<GroupMemberTable> list = DbController.getInstance().getRead().getGroupMemberTableDao().queryBuilder()
                .where(GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId()
                ), GroupMemberTableDao.Properties.GroupId.eq(groupId)).list();
        if (list != null) {
            DbController.getInstance().getWrite().getGroupMemberTableDao().deleteInTx(list);
        }
    }

    public String getLoginUserId() {
        return this.loginUserId;
    }

    public void setLoginUserId(String loginUserId) {
        this.loginUserId = loginUserId;
    }

    public String getGroupMemberName() {
        return this.groupMemberName;
    }

    public void setGroupMemberName(String groupMemberName) {
        this.groupMemberName = groupMemberName;
    }

    public String getGroupMemberAvatarId() {
        return this.groupMemberAvatarId;
    }

    public void setGroupMemberAvatarId(String groupMemberAvatarId) {
        this.groupMemberAvatarId = groupMemberAvatarId;
    }


    public Long getId() {
        return this.id;
    }


    public void setId(Long id) {
        this.id = id;
    }


    public String getGroupMemberId() {
        return this.groupMemberId;
    }


    public void setGroupMemberId(String groupMemberId) {
        this.groupMemberId = groupMemberId;
    }


    public String getGroupMemberUserId() {
        return this.groupMemberUserId;
    }


    public void setGroupMemberUserId(String groupMemberUserId) {
        this.groupMemberUserId = groupMemberUserId;
    }


    public String getGroupMemberAvatar() {
        return this.groupMemberAvatar;
    }


    public void setGroupMemberAvatar(String groupMemberAvatar) {
        this.groupMemberAvatar = groupMemberAvatar;
    }


    public int getGroupMemberType() {
        return this.groupMemberType;
    }


    public void setGroupMemberType(int groupMemberType) {
        this.groupMemberType = groupMemberType;
    }


    public String getGroupMemberNick() {
        return this.groupMemberNick;
    }


    public void setGroupMemberNick(String groupMemberNick) {
        this.groupMemberNick = groupMemberNick;
    }


    public String getGroupMemberAlias() {
        return this.groupMemberAlias;
    }


    public void setGroupMemberAlias(String groupMemberAlias) {
        this.groupMemberAlias = groupMemberAlias;
    }


    public String getUserType() {
        return this.userType;
    }


    public void setUserType(String userType) {
        this.userType = userType;
    }


    public String getGroupMemberAvatarPath() {
        return this.groupMemberAvatarPath;
    }


    public void setGroupMemberAvatarPath(String groupMemberAvatarPath) {
        this.groupMemberAvatarPath = groupMemberAvatarPath;
    }

    public static long getGroupMemberCount(String groupId) {
        return DbController.getInstance().getRead().getGroupMemberTableDao().queryBuilder()
                .where(GroupMemberTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId()
                ), GroupMemberTableDao.Properties.GroupId.eq(groupId)).count();
    }
}
